package com.vinoxm.rss.handler.interceptor;

import com.vinoxm.common.core.interceptor.CustomHandlerInterceptor;
import com.vinoxm.rss.handler.filter.ControllerPreCheckFilterChain;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Log4j2
public class ControllerInterceptor extends CustomHandlerInterceptor {

    @Setter(onMethod_ = {@Resource})
    private ControllerPreCheckFilterChain filterChain;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (handler instanceof HandlerMethod) {
            filterChain.doCheck(request, response, (HandlerMethod) handler);
        }
        log.info(String.format("[Access request] %s:%s ",request.getMethod(), request.getRequestURI()));
        return true;
    }
}
